import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import Users from "../components/users/Users";
//权限列表
import Rights from "../components/power/Rights";
import Roles from "../components/power/Roles";
import Cate from "../components/doods/Cate";
import Params from "../components/doods/Params";

//角色列表



Vue.use(VueRouter)

const routes = [
    //路由重定向  一访问/就到login页面
    {path: '/',redirect:'/login'},
    {path: '/login', component: Login },
    {path: '/home', component: Home,redirect:'/welcome' ,
        children:[
            {path: '/welcome', component: Welcome },
            {path: '/users', component: Users },
            {path: '/rights', component: Rights },
            {path: '/roles', component: Roles },
            {path: '/categories', component: Cate },
            {path: '/params', component: Params },
        ] }


]


const router = new VueRouter({
    routes
})


//挂载路由导航守卫
// 拿到路由对象，挂载到导航守卫beforeEach这个对象上   导航守卫有三个参数   to  from  next
router.beforeEach((to,from,next) =>{
//    to   将要访问的路径
//    from  代表从那个路径跳转而来
//    next   是一个函数，表示放行
//    next（）放行   next（'/login'）强制跳转
    if (to.path === '/login') return next()  //如果用户访问的是login  直接放行
//    如果不是 先获取token  判断token在做操作
    const token = window.sessionStorage.getItem('token')  //获取token
    if (!token) return next('/login')   //不是token  放行到login页面
    next ()   //token存在  直接放行
})
export default router
